<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.beaut.project.util.StaticData" %>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String type = request.getParameter("type") == null ? "" : request.getParameter("type");
	String recovery_id = request.getParameter("recovery_id") == null ? "" : request.getParameter("recovery_id");
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
	boolean isAdd = "".equals(recovery_id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>
<html>
	<head>
		<title><%=isAdd?"新建拆旧回收":"编辑拆旧回收" %></title>
 			<script  type="text/javascript"  src="<%=contextPath%>/core/js/datastructs.js"  ></script>
			<script  type="text/javascript"  src="<%=contextPath%>/core/js/sys.js"  ></script>
			<script  type="text/javascript"  src="<%=contextPath%>/core/js/prototype.js"  ></script>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extendTwo.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
		jQuery(document).ready(function(){
			jQuery("#mSearchValue").keypress(function(e) { 
		    	// 回车键事件 
		     	if(e.which == 13) { 
		     		 domSearch();
		       	} 
		   	}); 
			jQuery("#material_type_id").combotree({
				url:'<%=contextPath %>/springMVC/group/getSystemTypeTree?type=0',
				method:'POST',
				animate:true,
				lines:true,
				onChange:function(){
					domSearch();
				}
			});
			jQuery('#mtb').hide();
			if('<%=flag%>' == 1){
				jQuery('#ff').form('load','<%=contextPath %>/springMVC/gd/getRecoveryById?recovery_id=<%=recovery_id%>');
			}else{
				 jQuery("#operation_date").datetimebox('setValue',getTodaySecond());
			}
			
			jQuery('#table').datagrid({
				<%=isAdd?"url:'',":"url:'"+contextPath+"/springMVC/gd/listGDRecoveryDetail?recovery_id="+recovery_id+"',"%>
				rownumbers:true,
				fitColumns:true,
				frozenColumns:[[
					{field:'ck',checkbox:true},
				    {title:'产品id',field:'material_id',hidden:true}
				]],
				columns:[[
					{title:'产品编号',field:'code',width:100,align:'center',editor:{
						type:'combogrid',
						options:{
							url: '<%=contextPath %>/springMVC/warehouse/listPageMaterial',
						    panelWidth:750,
						    panelHeight:355,
						    rownumbers:true,
						    fitColumns:true,
						    columns: [[  
						       {field:'id',title:'主键',hidden:true},  
						       {field:'material_type_name',title:'产品类别',width:100,align:'center'},
						       {field:'code',title:'产品编号',width:100,align:'center'},
						       {field:'name',title:'产品名称',width:150,align:'center'},
						       {field:'format_type',title:'规格/型号',width:100,align:'center'},
						       {field:'unit_name',title:'单位',width:50,align:'center'}
						    ]],
						    onBeforeLoad:function(param){
								var mSearchValue = jQuery('#mSearchValue').val();
								var material_type_id = jQuery('#material_type_id').combotree('getValue');
								param.searchValue=mSearchValue;
								param.material_type_id=material_type_id;
							},
						    toolbar:'#mtb',
						    pageList : [10,50,100],  
							pagination:true,
							onClickRow:function(rowIndex,rowData){
									var pRow = jQuery('#table').datagrid('getRows')[lastIndex];
									pRow.material_id = rowData.id;
									pRow.code = rowData.code;
									pRow.name = rowData.name;
									pRow.unit_name = rowData.unit_name;
									pRow.format_type = rowData.format_type;
									jQuery('#table').datagrid('getData').rows[lastIndex]=pRow;
									var data = jQuery('#table').datagrid('getData');
									jQuery('#table').datagrid('loadData',data);
									
									jQuery('#table').datagrid('endEdit', lastIndex);
									jQuery('#table').datagrid('beginEdit', lastIndex);
									//lastIndex =-1;
							}
						}
					}},
					{title:'产品名称',field:'name',width:200,align:'center'},
					{title:'规格/型号',field:'format_type',width:200,align:'center'},
					{title:'单位',field:'unit_name',width:50,align:'center'},
					{title:'单价',field:'cost',width:100,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2
						}
					}},
					{title:'拆旧数量',field:'num',width:100,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2
						}
					}},
					{title:'备注',field:'remark',width:260,align:'center',editor:{
						type:'textarea'
					}}
				]],
				onBeforeLoad:function(){
					jQuery(this).datagrid('rejectChanges');
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						jQuery('#table').datagrid('endEdit', lastIndex);
						jQuery('#table').datagrid('beginEdit', rowIndex);
						var ed = jQuery('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = jQuery('#table').datagrid('getEditor', {index:rowIndex,field:'cost'});
						}
						jQuery(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				toolbar:[
					{
						id:'add',
						text:'选择材料',
						disabled:false,
						iconCls:'icon-add',
						handler:function(){
							addRow();
						}
					},'-',{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						jQuery('#table').datagrid('acceptChanges');
						lastIndex =-1;
					}
					},'-',{
					id:'delete',
					text:'删除',
					disabled:false,
					iconCls:'icon-remove',
					handler:function(){
						jQuery('#table').datagrid('acceptChanges');
						lastIndex =-1;
						var rows = jQuery('#table').datagrid('getSelections');
						if (rows.length > 0){
							for(var i=0;i<rows.length;i++){
								var row = rows[i];
								var index = jQuery('#table').datagrid('getRowIndex', row);
								jQuery('#table').datagrid('deleteRow', index);
							}
						}
						var data = jQuery('#table').datagrid('getData');
						jQuery('#table').datagrid('loadData',data);
					}
				}
			   ]
			});
		});
		function domSearch()
		{
			var mSearchValue = jQuery('#mSearchValue').val();
			var material_type_id = jQuery('#material_type_id').combotree('getValue');
			jQuery(combogridTarget).combogrid('grid').datagrid('load',{
				searchValue:mSearchValue,
				material_type_id:material_type_id
				});
		}
		function addRow(){
			jQuery('#table').datagrid('endEdit', lastIndex);
			jQuery('#table').datagrid('appendRow',{
				cost:'0',
				num:'0'
			});
			var data = jQuery('#table').datagrid('getData');
			jQuery('#table').datagrid('loadData',data);
			lastIndex = jQuery('#table').datagrid('getRows').length-1;
			jQuery('#table').datagrid('selectRow', lastIndex);
			jQuery('#table').datagrid('beginEdit', lastIndex);
		}
		
		function addGDMaterialRequest(obj){
			//自动提交数据
			  var gd_id=jQuery("#gd_id").val();
		      var gd_name=jQuery("#gd_name").val();
		      var gd_code=jQuery("#gd_code").val();
		      var dept_name=jQuery("#dept_name").val();
		      var dept_id=jQuery("#dept_id").val();
			jQuery('#table').datagrid('acceptChanges');
			lastIndex =-1;
			//自动提交数据
			if(jQuery('#ff').form('validate')){
				var products="";
				var jsondata=jQuery("#table").datagrid("getData");
				for(var i=0;i<jsondata.total;i++){
					if(products!=""){
						products+=";";
					}
					products+=jsonToStr(jsondata.rows[i]);
					var index = jQuery("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
					if(jsondata.rows[i].name == null || jsondata.rows[i].name == ""){
						jQuery.messager.alert('提示','第'+index+'行未选择产品！');
						return ;
					}else if(jsondata.rows[i].num == null || jsondata.rows[i].num == "" || jsondata.rows[i].num <= 0){
						jQuery.messager.alert('提示','第'+index+'行数量填写不正确！');
						return ;
					}
				}
				if(!checkNullOREmpty(products,'产品为必填项')){return;}
				var operation_date = jQuery("#operation_date").datetimebox('getValue');
				var remark = encodeURIComponent(jQuery('#remark').val());
				
				var param = "type=<%=type%>&gd_id="+gd_id+"&dept_id="+dept_id+"&dept_name="+dept_name+"&gd_code="+gd_code+"&gd_name="+gd_name+"&person_id=<%=person_id%>&person=<%=person%>&operation_date="+operation_date+"&remark="+remark+"&products="+products;
				jQuery(obj).hide();
				jQuery.ajax({
					type:'POST',
					<%=isAdd?"url:'"+contextPath+"/springMVC/gd/addGDRecovery',":"url:'"+contextPath+"/springMVC/gd/updateGDRecovery?recovery_id="+recovery_id+"',"%>
					data:param,
					dataType:'json',
					success:function(rtJson){
						if(rtJson.rtState == '0'){
							eval('window.opener.<%=callback%>();');
							window.close();
						}
						else{
							jQuery.messager.alert("提示",rtJson.rtMsrg);
						}
						jQuery(obj).show();
					}
				});
			}
		}
		 function choseGd(){
		    	openWindow("<%=contextPath %>/springViews/gd/choseGd.jsp?flagType=<%=StaticData.WORKING%>","工单选择",1000, 800);
		    	
		    }
			function rtCallBackGD(gd_id,gd_code,gd_name,dept_id,dept_name){
				  jQuery("#gd_id").val(gd_id);
			      jQuery("#gd_name").val(gd_name);
			      jQuery("#gd_code").val(gd_code);
			      jQuery("#dept_id").val(dept_id);
			      jQuery("#dept_name").val(dept_name);
			}
	</script>
	</head>
	<body>
		<div style="width: 97%;padding-left:10px">
		<form id="ff">
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
			<tr>
								<td align="right" class="td1" nowrap>工单编号：</td>
								<td class="td2" nowrap>
									<input type="hidden" id="gd_id" name="gd_id" >
									<input  id="gd_code" name="gd_code" readonly="readonly" onclick="choseGd();">
									<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:false" onclick="choseGd();"></a>								</td>
								<td align="right" class="td1" nowrap>工单名称：</td>
								<td class="td2" nowrap>
									<input  id="gd_name" name="gd_name" readonly="readonly" >
								</td>
							</tr>
				<tr>
					<td align="right" class="td1" nowrap>申请部门：</td>
					<td class="td2" nowrap>
						<input  id="dept_name" name="dept_name" readOnly >
						<input type="hidden" id="dept_id" name="dept_id"  />
					</td>
					<td align="right" class="td1" nowrap>申请人：</td>
					<td class="td2" nowrap>
						<input  id="person" name="person" readOnly value="<%=person%>">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>申请日期：</td>
					<td class="td2" nowrap colspan='3'>
						<input  id="operation_date" name="operation_date" class="easyui-datetimebox" data-options="required:true">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='3'>
						<textarea  id="remark" name="remark" rows="6" style="width: 100%"></textarea>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
						   <%if(!"1".equals(isShow)){ %>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addGDMaterialRequest(this);">提交</a>
							<%} %>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
						</div>
					</td>
				</tr>
				<tr>
					<td colspan="4"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
			</table>
			</form>
		</div>
		
			<div id="mtb" style="padding:3px;height:auto">
				<div align="right">
					分类：<input  id="material_type_id" name="material_type_id" class="easyui-combotree">
					查询条件：<input id="mSearchValue" name="mSearchValue" style="width:150px" title="材料编码或名称"/>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="domSearch();"></a>
				</div>
			</div>
		
	</body>
</html>